<?php

require_once './include/common.inc.php';
checkLogin();


getMyParam('categoryid');
getMyParam('subcategoryid');
$cate_id = $categoryid;


if (!is_numeric($categoryid) || $categoryid <=0) {
    header("Location:products.php");
}

$new_item_ary = array(
'id' => '1001',
'name' => 'New Items',
'pic' => 'new_items.jpg',
'seq' => '1001',
'description' => 'At jans2en our goal is to continuously discover, manufacture, and bring to our customers the very best selection in fine reproduction furnishings and home accessories on a continual basis. Visit this section of our web site on a regular basis to disco'
);


if ($subcategoryid>0) {
    $sql = "SELECT `name` FROM `category` WHERE `id` = $subcategoryid";
    $tmp_ary = $db->get_one($sql);
    $category_name = $tmp_ary['name'];
    
    $page_url = 'printall.php?categoryid=' . $categoryid . '&subcategoryid=' . $subcategoryid . '&';
    $sql = "SELECT p.`id` FROM `product` p, `product_category` c WHERE p.`id`=c.`product_id` AND c.`category_id` = $subcategoryid AND p.`hidden_flag`=0 GROUP BY p.`id`";
    $query = $db->query($sql);
    $sql_all_num = $db->num_rows($query);

    $ppp = 6;
    getMyParam('page');
    $page = empty($page) || !ispage($page) ? 1 : $page;
    $start_limit = ($page - 1) * $ppp;
    if($start_limit > $sql_all_num) {
        $start_limit = 0;
        $page = 1;
    }

    //$page_show = getPhotoPageStr($sql_all_num, $page, $ppp, $page_url);
    
    $photo_ary = array();
    $sql = "SELECT p.`id`, p.`code`, p.`name`, p.`price_1`, p.`price_2`, p.`price_3`, p.`small_pic`, p.`pic` FROM `product` p, `product_category` c WHERE p.`id`=c.`product_id` AND c.`category_id` = $subcategoryid AND p.`hidden_flag`=0 GROUP BY p.`id` ORDER BY c.`seq` ASC, p.`id` DESC LIMIT $start_limit, $ppp";
    $query = $db->query($sql);
    while($rs = $db->fetch_array($query)) {
        $out_ary[] = $rs;
    }
} elseif ($categoryid>1000) {
    
    //$out_ary[] = $new_item_ary;
    $category_name = 'New Items';
    if ($categoryid == 1001) {
        $categor_name = "New Items";
        $sql_where1 = '`new_flag`=1';
    }
    
    $page_url = 'printall.php?categoryid=' . $categoryid . '&';
    $sql = "SELECT p.`id` FROM `product` p, `product_category` pc, `category` c WHERE c.`id`=pc.`category_id` AND pc.`product_id`=p.`id` AND p.`new_flag`=1 AND p.`hidden_flag`=0 GROUP BY p.`id`"; 
    $query = $db->query($sql);
    $sql_all_num = $db->num_rows($query);

    $ppp = 6;
    getMyParam('page');
    $page = empty($page) || !ispage($page) ? 1 : $page;
    $start_limit = ($page - 1) * $ppp;
    if($start_limit > $sql_all_num) {
        $start_limit = 0;
        $page = 1;
    }
    
    
    $photo_ary = array();
    $sql = "SELECT p.`id`, p.`code`, p.`name`, p.`pic`, p.`small_pic`, p.`new_flag` FROM `product` p, `product_category` pc, `category` c WHERE c.`id`=pc.`category_id` AND pc.`product_id`=p.`id` AND p.`new_flag`=1 AND p.`hidden_flag`=0 GROUP BY p.`id` ORDER BY c.fid ASC, c.seq ASC, `code` DESC LIMIT $start_limit, $ppp"; 
    $query = $db->query($sql);
    while($rs = $db->fetch_array($query)) {
        $out_ary[] = $rs;
    }
    //echo "<pre>";print_r($out_ary);
} else {
    
    $sql = "SELECT `name` FROM `category` WHERE `id` = $cate_id";
    $tmp_ary = $db->get_one($sql);
    $category_name = $tmp_ary['name'];

    $categoryid_str = $categoryid;
    $sql = "SELECT `id`, `name`, `pic`, `pic_2`, `seq`, `description` FROM `category` WHERE `fid` = $cate_id ORDER BY `seq` ASC";
    $query = $db->query($sql);
    while($rs = $db->fetch_array($query)) {
        $sub_out_ary[] = $rs;
        $categoryid_str .= ',' . $rs['id'];
        if ($categoryid == $rs['id']) {
            //$category_name = $rs['name'];
            $category_pic_2 = $rs['pic_2'];
        }
    }
    
    
    $page_url = 'printall.php?categoryid=' . $categoryid . '&';
    $sql = "SELECT p.`id` FROM `product` p, `product_category` c WHERE p.`id`=c.`product_id` AND c.`category_id` IN (" . $categoryid_str . ") AND p.`hidden_flag`=0 GROUP BY p.`id`";
    $query = $db->query($sql);
    $sql_all_num = $db->num_rows($query);

    $ppp = 6;
    getMyParam('page');
    $page = empty($page) || !ispage($page) ? 1 : $page;
    $start_limit = ($page - 1) * $ppp;
    if($start_limit > $sql_all_num) {
        $start_limit = 0;
        $page = 1;
    }

    //$page_show = getPhotoPageStr($sql_all_num, $page, $ppp, $page_url);
    
    $photo_ary = array();
    $sql = "SELECT p.`id`, p.`code`, p.`name`, p.`price_1`, p.`price_2`, p.`price_3`, p.`small_pic`, p.`pic` FROM `product` p, `product_category` c WHERE p.`id`=c.`product_id` AND c.`category_id` IN (" . $categoryid_str . ") AND p.`hidden_flag`=0 GROUP BY p.`id` ORDER BY c.`seq` ASC, p.`id` DESC LIMIT $start_limit, $ppp";
    $query = $db->query($sql);
    while($rs = $db->fetch_array($query)) {
        $out_ary[] = $rs;
    }
}


$print_title = $category_name;
$page_title = "jans2en furniture - Products - $category_name - All products";

$select_page = '';
$pages = @ceil($sql_all_num / $ppp);
$start = ($page-1)*$ppp+1;
$end = $page<$pages ? $start+5 : $sql_all_num;
for ($i=1; $i<=$pages; $i++) {
    if ($i == $page) {
        $select_page .= '<option value="' . $i . '" selected>' . $i . '</option>'; 
    } else {
        $select_page .= '<option value="' . $i . '">' . $i . '</option>';   
    } 
}

//$page_url = 'printall.php?categoryid=' . $categoryid . '&';
$page_show = getPhotoPageStr($sql_all_num, $page, $ppp, $page_url);


require_once PrintPrint('allproduct');
require_once PrintPrint('footer');

?>